﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：Program.cs
// 功能描述：TongTongMall.RabbitMq.ConsumerTest
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TongTongMall.RabbitMq.ConsumerTest
{
    class Program
    {
        public static void Main()
        {
            //var rpcClient = new RPCClient();

            //Console.WriteLine(" [x] Requesting fib(30)");
            //var response = rpcClient.Call("30");
            //Console.WriteLine(" [.] Got '{0}'", response);

            //rpcClient.Close();
        }
    }

    //class RPCClient
    //{
    //    private ICollection connection;
    //    private IModel channel;
    //    private string replyQueueName;
    //    private QueueingBasicConsumer consumer;

    //    public RPCClient()
    //    {
    //        var factory = new ConnectionFactory() { HostName = "localhost" };
    //        connection = factory.CreateConnection();
    //        channel = connection.CreateModel();
    //        replyQueueName = channel.QueueDeclare().QueueName;
    //        consumer = new QueueingBasicConsumer(channel);
    //        channel.BasicConsume(queue: replyQueueName,
    //                             noAck: true,
    //                             consumer: consumer);
    //    }

    //    public string Call(string message)
    //    {
    //        var corrId = Guid.NewGuid().ToString();
    //        var props = channel.CreateBasicProperties();
    //        props.ReplyTo = replyQueueName; 
    //        props.CorrelationId = corrId;

    //        var messageBytes = Encoding.UTF8.GetBytes(message);
    //        channel.BasicPublish(exchange: "",
    //                             routingKey: "rpc_queue",
    //                             basicProperties: props,
    //                             body: messageBytes);

    //        while (true)
    //        {
    //            var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
    //            if (ea.BasicProperties.CorrelationId == corrId)
    //            {
    //                return Encoding.UTF8.GetString(ea.Body);
    //            }
    //        }
    //    }

    //    public void Close()
    //    {
    //        connection.Close();
    //    }
    //}
}
